1B - Spreadsheet - CodeForces Solution


implementation math *1600

Please click on ads to support us..

Python Code:

n = int(input())
alpha = "ZABCDEFGHIJKLMNOPQRSTUVWXY"
for _ in range(n):
    s = input()
    ft = 1 if s[0] == "R" and s[1].isdigit() and "C" in s else 2

    if ft == 1:
        idx = s.index("C")
        r = (s[1:idx])
        c = int(s[idx+1:])
        cs = ""
        while c > 0:
            cs = alpha[c % 26] + cs
            if c % 26 == 0: c-=1
            c = c//26
        print(cs+r)
    else:
        for i in range(len(s)):
            if s[i].isdigit():
                break
        cs = s[:i]
        r = s[i:]
        csi = 0
        for c in cs:
            idx = alpha.index(c)
            idx = idx if idx != 0 else 26
            csi *= 26
            csi+=idx
        print("R"+r+"C"+str(csi))

C++ Code:

#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;
typedef long long int ll; 
typedef unsigned long long ull;
typedef long double ld;       
#define pb push_back
#define pob pop_back
#define ff first
#define ss second
#define all(v) (v).begin(), (v).end()
#define sare(v) (v).begin(), (v).end()
#define MOD 1000000007
//#define fastio() ios_base::sync_with_stdio(0), cin.tie(0)
const int N=5e5+10;


//------------------------THINGS_TO_REMEMBER---------------------------------
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//########   -> M_PI = value of pi
//########   -> INT_MAX=1e18
//########   -> ll = 10^-18 - 10^18


//-----------------------DEFINED FUNCTIONS----------------------------------
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$


ll gcd(ll a, ll b)
{if(max(a,b)%min(a,b)==0) return min(a,b);
	while(1)
	{  if(a>b) {a=a-b;   if(b%a==0) return a;}
		else {b=b-a; if(a%b==0) return b;}   }}

bool prime(ll &n)
{
    if (n <= 1)  return false;
    if (n <= 3) return true;
    if (n % 2 == 0 || n % 3 == 0)  return false;
    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)  return false;
    return true;
}
//---------------------------DFS---------------------------------
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

/*
vector <ll> g[N];
vector <ll> visited(N);
void dfs(ll vertex)
{
	// action on vertex after entering
	if(visited[vertex]) return;
	
	visited[vertex]++;
	
	for(auto child: g[vertex])
	{
		// action on child before entering child

		dfs(child);
		
		
		// action on child after exiting the child
		
	}
	// action on vertex after before exiting vertex
}

*/



//---------------------------BFS---------------------------------
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$


/*
vector <ll> g[N];
vector <ll> visited(N);
vector <ll> level(N);
void bfs(ll source)
{
	queue <ll> q;
	q.push(source);
	visited[source]=1;
	
	while(!q.empty())
	{
		ll curr_v = q.front();
		q.pop();
		for(ll child:g[curr_v])
		{
			if(!visited[child])
			{
				q.push(child);
				visited[child]++;
				level[child]=level[curr_v]+1;
			}
		}
	}
}


*/

//---------------------------seive-----------------------------------------
/*
bool se[N];
for(ll i=2;i<=N;i++)
	{	
		se[i]=true;	
	}
	
for(ll i=2;i<=N;i++)
	{	
		if(se[i]==true)
		{
			for(ll j=i*i;j<=N;j+=i)
			se[j]=false;
		}	
	}
	
*/
//----------------------BINARY INDEX TREE---------------------------------
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
/*deque <ll> bit(N);
ll n;
void update1(ll i,ll x)
{
	ll ans=x*i;
	for(;i<=n;i+=(i&(-i)))
	{
		bit[i]=ans;
		ans+=x;
	}
}
void update (ll i,ll x)
{
	for(;i<=n;i+=(i&(-i)))
	{
		bit[i]+=x;
	}
}
ll sum(ll i)
{
	ll ans=0;
	for(;i>0;i-=(i&(-i)))
	{
		ans+=bit[i];
	}
	return ans;
}
*/
string code(ll n)
{
	string ans;
	if(n<=26)
	{
		char c='A'+(n-1);
		ans.pb(c);
	}
	while(n>26)
	{
		ll r=n%26;
		n=n/26;
		if(r==0)
		{
			ans.pb('Z');
			n--;
		}
		else
		{
			char c='A'+r; c--;
			ans.pb(c);
		}
		if(n<=26)
		{
			char c='A'+(n-1);
			ans.pb(c);
		}
	}
	reverse(all(ans));
	return ans;
}

ll decode(string s)
{
	ll ans=0,t=1;
	for(int i=s.size()-1;i>=0;i--)
	{
		ans=ans+((s[i]-'A')+1)*t;
		t*=26;
	}
	return ans;
}
//--------------------------------solve---------------------------------
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

void solve()
{
	string s;     bool f=true;
	cin>>s;
	map <char,ll> mp;
	
	for(int i=0;i<s.size();i++)
	{
		mp[s[i]]++;
	}
	if(mp['R']&&mp['C']&&(s[1]<'A'||s[1]>'Z')) f=false;
	//f=true = type 1
	
	if(f)
	{
		ll i=0,temp=0;
		string a;
		while(s[i]>='A'&&s[i]<='Z') 
		{
			a.pb(s[i]);
			i++;
		}
		while(i<s.size())
		{
			temp=temp*10+(s[i]-'0'); 
			i++;
		}
		cout<<"R"<<temp<<"C"<<decode(a)<<endl;
		
	}
	
	//f=false = type 2
	
	else 
	{
		ll i=1,x=0,y=0;
		while(s[i]!='C') 
		{
			x=x*10+(s[i]-'0');
			i++;
		}
		i++;
		while(i<s.size())
		{
			y=y*10+(s[i]-'0');
			i++;
		}
		cout<<code(y)<<x<<endl;
			
	}
	
	
}
//------------------------INT_MAIN-------------------------------------
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
int main()
{
   int T = 1;
   cin >> T;
   while(T--){
  		solve();
  		
   }
    return 0;
}
     		 			 		 	 	    	     	


Comments

Submit
0 Comments
More Questions

1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome
3B - Lorry
1392A - Omkar and Password
489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut